//
//  ByBasketBallBetVC.m
//  caipiao_ios
//
//  Created by mc on 2018/8/20.
//  Copyright © 2018年 ZhangZhanbin. All rights reserved.
//

#import "ByBasketBallBetVC.h"
#import "ByFootBallTypeView.h"
#import "ByFootBallBottomView.h"
#import "ByBasketBallListVC.h"
#import "ByBasketBallMixedCell.h"
#import "ByBasketBallMixedPopView.h"
#import "ByBasketBallSFCell.h"
#import "ByBasketBallSFCCell.h"
#import "ByFootBallSecitionView.h"


@interface ByBasketBallBetVC ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) UIButton *titleBtn;
@property (nonatomic, strong) ByFootBallTypeView *typeView;
@property (nonatomic, strong)UIButton  *   maskView;// 背景
@property (nonatomic, assign) NSInteger type;// 0 混合投注 1 胜负 2.让胜负 3 大小分 4 胜负差
@property (nonatomic, strong) NSMutableArray *sectionStateArray;
@property (nonatomic, strong) ByFootBallBottomView *bottomView;

@property(nonatomic,strong) NSMutableArray * sfArray;
@property(nonatomic,strong) NSMutableArray * rsfArray;
@property(nonatomic,strong) NSMutableArray * bigSmallArray;
@property(nonatomic,strong) NSMutableArray * sfcArray;
@property (nonatomic, assign) NSInteger numberOfMatch;

@property (nonatomic,strong) NSArray *timesArray;

@end

@implementation ByBasketBallBetVC


- (ByFootBallTypeView *)typeView{
    if (_typeView == nil) {
        _typeView = [[ByFootBallTypeView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 110)];
        _typeView.type = self.type;
        NSArray *itemsAry = @[@"混合投注",@"胜负",@"让分胜负",@"大小分",@"胜负差"];
        _typeView.typeArray = [itemsAry copy];
        __weak typeof(self) weakSelf = self;
        _typeView.indexAtView = ^(NSInteger index){
            [weakSelf.titleBtn setTitle:[itemsAry objectAtIndex:index] forState:UIControlStateNormal];
            [weakSelf maskAction:nil];
            weakSelf.tableView.contentOffset = CGPointMake(0, 0);
            weakSelf.type = index;
            [weakSelf deselectData];
            [weakSelf.tableView reloadData];
        };
    }
    return _typeView;
}

- (UIButton *)maskView{
    if (_maskView == nil) {
        _maskView = [UIButton buttonWithType:UIButtonTypeCustom];
        _maskView.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
        _maskView.backgroundColor = [UIColor colorWithRed:100/255.0 green:100/255.0 blue:100/255.0 alpha:0.2];
        [_maskView addTarget:self action:@selector(maskAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _maskView;
}

- (void)maskAction:(UIButton *)btn{
    
    [_typeView removeFromSuperview];
    _typeView = nil;
    CGAffineTransform  transform = CGAffineTransformRotate(self.titleBtn.imageView.transform,M_PI);
    [UIView beginAnimations:@"1" context:nil];
    [UIView setAnimationDuration:0.5];
    [self.titleBtn.imageView setTransform:transform];
    [UIView commitAnimations];
    [_maskView removeFromSuperview];
    _maskView = nil;
}


- (void)titleViewAction:(UIButton *)btn{
    
    if (_typeView) {
        
        [_maskView removeFromSuperview];
        [_typeView removeFromSuperview];
        _maskView = nil;
        _typeView = nil;
        
    }else{
        
        [self.view addSubview:self.maskView];
        [self.view addSubview:self.typeView];
    }
    
    
    CGAffineTransform  transform = CGAffineTransformRotate(self.titleBtn.imageView.transform,M_PI);
    [UIView beginAnimations:@"1" context:nil];
    [UIView setAnimationDuration:0.5];
    [self.titleBtn.imageView setTransform:transform];
    [UIView commitAnimations];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self addLeftBackBarButtonItem];
    [self addBYItemviews:@[@"玩法说明"] mode:ByNavigationItemModeText position:ByNavigationItemPositionModeRight];
    
    [self initViews];
    
    [self loadOriginalData];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refreshData:) name:@"refreshBasketBallData" object:nil];
    
    // Do any additional setup after loading the view.
}

- (void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)refreshData:(NSNotification *)sender{
    NSString *str = sender.object;
    NSLog(@"足球数据 = %@", str);
    [self deselectDataWithStr:str];
    [self.tableView reloadData];
}



- (void)deselectDataWithStr:(NSString *)str{
    
    [ByFootBallHandle deselectBallDataWithArray:self.sfArray WithRemainStr:str];
    [ByFootBallHandle deselectBallDataWithArray:self.rsfArray WithRemainStr:str];
    [ByFootBallHandle deselectBallDataWithArray:self.bigSmallArray WithRemainStr:str];
    [ByFootBallHandle deselectBallDataWithArray:self.sfcArray WithRemainStr:str];
    [self updateBottomState];
}


- (void)rightItemsAction:(NSInteger)rightIndex{
    
    ByWebViewController *vc = [[ByWebViewController alloc] initWithTotalUrlString:[NSString stringWithFormat:@"%@/Wanfajiesao/jiesao?type=3001",BOYE_WEB_BASE_URL]];
    vc.titleString = @"竞彩篮球玩法说明";
    [self by_normalPush:vc hasNavi:YES];
    
}


- (void)deselectData{
    [ByFootBallHandle deselectFootBallDataWithArray:self.sfArray];
    [ByFootBallHandle deselectFootBallDataWithArray:self.rsfArray];
    [ByFootBallHandle deselectFootBallDataWithArray:self.bigSmallArray];
    [ByFootBallHandle deselectFootBallDataWithArray:self.sfcArray];
}
- (void)loadOriginalData{
    
    self.sectionStateArray = [NSMutableArray arrayWithCapacity:1];
    [self.sectionStateArray removeAllObjects];
    ByNetModel *netModel = [[ByNetModel alloc] init];
    [ByCoreToolAlert showLoadind:@"加载中" hasMasker:YES];
    [ByBallApi getBasketBallMatch:netModel success:^(ByNetBaseResult *result) {
        [ByCoreToolAlert hideHud];
        SLog(@"蓝球赛事 = %@", result.data);
        NSDictionary *dictionary = result.data[@"data"];
        if (![dictionary isKindOfClass:[NSDictionary class]]) {
            [ByCoreToolAlert showOnlyText:@"暂无比赛数据"];
            [self.tableView reloadData];
            return ;
        }
        NSArray *timesArray = [dictionary allKeys];
        self.timesArray = [timesArray sortedArrayUsingComparator:^NSComparisonResult(id  _Nonnull obj1, id  _Nonnull obj2) {
            return [obj1 compare:obj2 options:NSNumericSearch];
        }];
        
        
        
        NSMutableArray *sfArray = [NSMutableArray arrayWithCapacity:1];
        NSMutableArray *rsfArray = [NSMutableArray arrayWithCapacity:1];
        NSMutableArray *bigSmallArray = [NSMutableArray arrayWithCapacity:1];
        NSMutableArray *sfcArray = [NSMutableArray arrayWithCapacity:1];
        
    for (int i = 0; i < self.timesArray.count; i++) {
        [self.sectionStateArray  addObject:@(1)];
        NSString *timeString = self.timesArray[i];
        NSArray *array = dictionary[timeString];
        
        NSMutableArray *sfSectionArray = [NSMutableArray arrayWithCapacity:1];
        NSMutableArray *rsfSectionArray = [NSMutableArray arrayWithCapacity:1];
        NSMutableArray *bigSmallSectionArray = [NSMutableArray arrayWithCapacity:1];
        NSMutableArray *sfcSectionArray = [NSMutableArray arrayWithCapacity:1];
        
        for (NSDictionary *dic in array) {
            if (dic[@"HDC"]) {//让分胜负
                ByNetModel *HDC_Model = [[ByNetModel alloc] initContentWithDic:dic[@"HDC"]];
                NSMutableArray *rowRsfArray = [ByBasketBallHandle getFinalRsfDataWithDic:dic WithOtherModel:HDC_Model];
                rowRsfArray=(NSMutableArray *)[[rowRsfArray reverseObjectEnumerator] allObjects];
                [rsfSectionArray addObject:rowRsfArray];
            }else{
                NSMutableArray *dataArray = [ByFootBallHandle getNoSaleDataWith:dic WithCount:2];
                dataArray=(NSMutableArray *)[[dataArray reverseObjectEnumerator] allObjects];
                [rsfSectionArray addObject:dataArray];
            }
            
            if (dic[@"HILO"]) { //大小分
                ByNetModel *HILO_Model = [[ByNetModel alloc] initContentWithDic:dic[@"HILO"]];
                NSMutableArray *rowBigArray = [ByBasketBallHandle getFinalBigSmallDataWithDic:dic WithOtherModel:HILO_Model];
                [bigSmallSectionArray addObject:rowBigArray];
            }else{
                NSMutableArray *dataArray = [ByFootBallHandle getNoSaleDataWith:dic WithCount:2];
                [bigSmallSectionArray addObject:dataArray];
            }
            
            if (dic[@"MNL"]) {//胜负
                ByNetModel *MNL_Model = [[ByNetModel alloc] initContentWithDic:dic[@"MNL"]];
                NSMutableArray *rowSfArray = [ByBasketBallHandle getFinalSfDataWithDic:dic WithOtherModel:MNL_Model];
                rowSfArray=(NSMutableArray *)[[rowSfArray reverseObjectEnumerator] allObjects];
                [sfSectionArray addObject:rowSfArray];
                
            }else{
                NSMutableArray *dataArray = [ByFootBallHandle getNoSaleDataWith:dic WithCount:2];
                dataArray=(NSMutableArray *)[[dataArray reverseObjectEnumerator] allObjects];
                [sfSectionArray addObject:dataArray];
            }
            
    
            if (dic[@"WNM"]) {//胜分差
                ByNetModel *WNM_Model = [[ByNetModel alloc] initContentWithDic:dic[@"WNM"][@"special_odds"]];
                NSString *single =  dic[@"WNM"][@"single"];
                WNM_Model.single = single;
                NSMutableArray *rowSfcArray = [ByBasketBallHandle getFinalSfcDataWithDic:dic WithOtherModel:WNM_Model];
                [sfcSectionArray addObject:rowSfcArray];
            }else{
                NSMutableArray *dataArray = [ByFootBallHandle getNoSaleDataWith:dic WithCount:12];
                [sfcSectionArray addObject:dataArray];
            }
        }
          [sfArray addObject:sfSectionArray];
          [rsfArray addObject:rsfSectionArray];
          [bigSmallArray addObject:bigSmallSectionArray];
          [sfcArray addObject:sfcSectionArray];
            
        }
        
        self.sfArray = sfArray;
        self.rsfArray = rsfArray;
        self.bigSmallArray = bigSmallArray;
        self.sfcArray = sfcArray;
        
        [self.tableView reloadData];
        
        
    } failure:^(ByNetBaseResult *result) {
        [ByCoreToolAlert showFailureWithText:result.msg];
    }];
    
}

- (void)initViews{
    
    self.titleBtn = [self createButtonWithFrame:CGRectMake((SCREEN_WIDTH - 130) / 2, 7, 130, 30) backgroundColor:[UIColor clearColor] title:@"混合过关" titleColor:[UIColor whiteColor] font:FONT(14) target:self action:@selector(titleViewAction:)];
    [self.titleBtn setImage:[UIImage imageNamed:@"mingxifenlei"] forState:UIControlStateNormal];
    self.titleBtn.imageEdgeInsets = UIEdgeInsetsMake(10, 115, 10, 0);
    self.titleBtn.titleEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 12);
    UINavigationBar * bar = self.navigationController.navigationBar;
    [bar addSubview:self.titleBtn];
    
    self.tableView = [[UITableView alloc] init];
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(0);
        make.right.equalTo(self.view).offset(0);
        if (@available(ios 11.0,*)) {
            make.top.equalTo(self.view.mas_safeAreaLayoutGuideTop).offset(0);
            make.bottom.equalTo(self.view.mas_safeAreaLayoutGuideBottom).offset(-49);
        } else {
            make.top.equalTo(self.view).offset(0);
            make.bottom.equalTo(self.view).offset(-49);
        }
    }];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.estimatedSectionHeaderHeight = 0;
    self.tableView.estimatedSectionFooterHeight = 0;
    self.tableView.estimatedRowHeight = 0;
    self.tableView.backgroundColor = BY_BACKGROUND_COLOR;
    
    ByFootBallBottomView *bottomView = [[ByFootBallBottomView alloc] init];
    [self.view addSubview:bottomView];
    [bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(0);
        make.right.equalTo(self.view).offset(0);
        make.top.equalTo(self.tableView.mas_bottom).offset(0);
        if (@available(ios 11.0,*)) {
        make.bottom.equalTo(self.view.mas_safeAreaLayoutGuideBottom).offset(0);
        } else {
        make.bottom.equalTo(self.view).offset(0);
        }
        
    }];
    self.bottomView = bottomView;
    
    bottomView.clearBlock = ^() {
        [self deselectData];
        [self.tableView reloadData];
        [self updateBottomState];
        
    };
    
    // 0 混合投注 1 胜负 2.让胜负 3 大小 4 胜负差
    bottomView.sureBlock = ^(){
        
        
        NSMutableArray *selectedArray1 = [ByFootBallHandle getFootBallSelectedDataWithArray:[self.sfArray copy]];
        NSMutableArray *selectedArray2 = [ByFootBallHandle getFootBallSelectedDataWithArray:[self.rsfArray copy]];
        NSMutableArray *selectedArray3 = [ByFootBallHandle getFootBallSelectedDataWithArray:[self.bigSmallArray copy]];
        NSMutableArray *selectedArray4 = [ByFootBallHandle getFootBallSelectedDataWithArray:[self.sfcArray copy]];
       
        
        
        if (self.numberOfMatch == 1) {
            
            ByNetModel *onlyModel;
            if (selectedArray1.count > 0) {
                onlyModel = [selectedArray1 firstObject];
            }else if (selectedArray2.count > 0){
                onlyModel = [selectedArray2 firstObject];
            }else if (selectedArray3.count > 0){
                onlyModel = [selectedArray3 firstObject];
            }else if (selectedArray4.count > 0){
                onlyModel = [selectedArray4 firstObject];
            }
            
            
            
            BOOL value1 = [ByFootBallHandle oneGameIsOnlyHasSinglePass:selectedArray1];
            BOOL value2 = [ByFootBallHandle oneGameIsOnlyHasSinglePass:selectedArray2];
            BOOL value3 = [ByFootBallHandle oneGameIsOnlyHasSinglePass:selectedArray3];
            BOOL value4 = [ByFootBallHandle oneGameIsOnlyHasSinglePass:selectedArray4];
        
            
            if (value1 && value2 && value3 && value4) {
                
            }else{
                [ByCoreToolAlert showOnlyText:@"至少选择两场比赛"];
                return ;
            }
            
            
            NSLog(@"唯一一场比赛 single = %@， title = %@", onlyModel.single,onlyModel.title);
            if (onlyModel.single.integerValue > 0) {
                
                
            }else{
                
                if (self.numberOfMatch < 2) {
                    [ByCoreToolAlert showOnlyText:@"至少选择两场比赛"];
                    return ;
                }
                
            }
            
            
        }else{
            
            if (self.numberOfMatch < 2) {
                [ByCoreToolAlert showOnlyText:@"至少选择两场比赛"];
                return ;
            }
            
            if (self.numberOfMatch > 8) {
                [ByCoreToolAlert showOnlyText:@"最多选择8场比赛"];
                return;
            }
        }
        
        
        NSMutableDictionary *sfDic =  [ByBasketBallHandle getFootBallDictionaryOfMatch:selectedArray1];
        NSMutableDictionary *rsfDic =  [ByBasketBallHandle getFootBallDictionaryOfMatch:selectedArray2];
        NSMutableDictionary *bigSmallDic =  [ByBasketBallHandle getFootBallDictionaryOfMatch:selectedArray3];
        NSMutableDictionary *sfcDic =  [ByBasketBallHandle getFootBallDictionaryOfMatch:selectedArray4];

        
        NSMutableArray *keysArray = [NSMutableArray arrayWithCapacity:1];
        NSArray *sfKeys = [sfDic allKeys];
        NSArray *rsfKeys = [rsfDic allKeys];
        NSArray *bigSmallKeys = [bigSmallDic allKeys];
        NSArray *sfcKeys = [sfcDic allKeys];
        
        [keysArray addObjectsFromArray:sfKeys];
        [keysArray addObjectsFromArray:rsfKeys];
        [keysArray addObjectsFromArray:bigSmallKeys];
        [keysArray addObjectsFromArray:sfcKeys];
       
        
        NSSet *set = [NSSet setWithArray:keysArray];
        NSMutableArray *allObjects = [NSMutableArray arrayWithArray:set.allObjects];
        [allObjects sortUsingComparator:^NSComparisonResult(NSString * obj1, NSString * obj2) {
            return [obj1 compare:obj2];
        }];
        
        NSMutableArray *dataArray = [NSMutableArray arrayWithCapacity:1];
        
        for (int i = 0; i < set.count; i++){
            ByNetModel *netModel = [[ByNetModel alloc] init];
            
            NSString *key = allObjects[i];
            if ([rsfDic objectForKey:key]) {
                netModel.rsfArray = [ByFootBallHandle getFootBallAllRowDataArrayWithKey:key WithArray:self.rsfArray];
                NSMutableArray *selectRowRsfArray = [rsfDic objectForKey:key];
                netModel.netModel = [[selectRowRsfArray firstObject] copy];
                
            }
            
            if ([sfDic objectForKey:key]) {
                netModel.sfArray = [ByFootBallHandle getFootBallAllRowDataArrayWithKey:key WithArray:self.sfArray];
                NSMutableArray *selectRowSfArray = [sfDic objectForKey:key];
                netModel.netModel = [[selectRowSfArray firstObject] copy];
            }
            
            if ([bigSmallDic objectForKey:key]) {
                netModel.bigSmallArray = [ByFootBallHandle getFootBallAllRowDataArrayWithKey:key WithArray:self.bigSmallArray];
                NSMutableArray *selectRowbigSmallArray = [bigSmallDic objectForKey:key];
                netModel.netModel = [[selectRowbigSmallArray firstObject] copy];
            }
            
            if ([sfcDic objectForKey:key]) {
                netModel.sfcArray = [ByFootBallHandle getFootBallAllRowDataArrayWithKey:key WithArray:self.sfcArray];
                NSMutableArray *selectRowSfcArray = [sfcDic objectForKey:key];
                netModel.netModel = [[selectRowSfcArray firstObject] copy];
            }
            
            [dataArray addObject:netModel];
        }
        
        NSLog(@"dataArray = %@", dataArray);
        
        ByBasketBallListVC *vc = [[ByBasketBallListVC alloc] init];
        vc.type = self.type;
        vc.dataArray = dataArray;
        [self by_normalPush:vc hasNavi:YES];
        
    };
    
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.sectionStateArray.count;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    NSNumber *number = self.sectionStateArray[section];
    NSMutableArray *sectionAry = [self.sfArray objectAtIndex:section];
    if (number.integerValue > 0) {
        return sectionAry.count;
    }else{
        return 0;
    }
    
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSMutableArray *rowSfArray = [[self.sfArray objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
    NSMutableArray *rowRsfArray = [[self.rsfArray objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
    NSMutableArray *rowBigSmallArray = [[self.bigSmallArray objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
    NSMutableArray *rowSfcArray = [[self.sfcArray objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
    //混合投注
    if (self.type == 0) {
        
        ByBasketBallMixedCell *cell = [ByBasketBallMixedCell cellWithTableView:tableView];
        NSMutableArray *dataArray = [NSMutableArray arrayWithCapacity:1];
        [dataArray addObjectsFromArray:[rowSfArray copy]];
        [dataArray addObjectsFromArray:[rowRsfArray copy]];
        cell.dataArray = dataArray;
        
        cell.indexAtView = ^(NSInteger index){
            
            ByNetModel *netModel = [dataArray objectAtIndex:index];
            netModel.selected = !netModel.selected;
            [self.tableView reloadData];
            [self updateBottomState];
        };
        [self getSelectedCountReloadBtn:cell.morePlayTypeBtn AtIndexPath:indexPath];
        cell.morePlayBlock = ^(){
            
            ByBasketBallMixedPopView *popView = [[ByBasketBallMixedPopView alloc] init];
            [popView setDataSfArray:rowSfArray AndRsfArray:rowRsfArray AndBigSmallArray:rowBigSmallArray AndSfcArray:rowSfcArray];
            popView.frame = CGRectMake(popView.frame.origin.x, popView.frame.origin.y-SCREEN_HEIGHT, popView.frame.size.width, popView.frame.size.height);
            
            [UIView animateWithDuration:0.5 animations:^{
                popView.frame = CGRectMake(popView.frame.origin.x, popView.frame.origin.y+SCREEN_HEIGHT, popView.frame.size.width, popView.frame.size.height);
                
            }];
            
            
            popView.selectedBlock = ^(NSMutableArray *sfArray, NSMutableArray *rsfArray, NSMutableArray *bigSmallArray, NSMutableArray *sfcArray){
                NSMutableArray *sfSectionArray = [self.sfArray objectAtIndex:indexPath.section];
                NSMutableArray *rsfSectionArray = [self.rsfArray objectAtIndex:indexPath.section];
                NSMutableArray *bigSectionArray = [self.bigSmallArray objectAtIndex:indexPath.section];
                NSMutableArray *sfcSectionArray = [self.sfcArray objectAtIndex:indexPath.section];
                
                [sfcSectionArray replaceObjectAtIndex:indexPath.row withObject:sfcArray];
                [sfSectionArray replaceObjectAtIndex:indexPath.row withObject:sfArray];
                [bigSectionArray replaceObjectAtIndex:indexPath.row withObject:bigSmallArray];
                [rsfSectionArray replaceObjectAtIndex:indexPath.row withObject:rsfArray];
                
                [self.tableView reloadData];
                [self updateBottomState];
            };
            
        };
        return cell;
        
    //胜负 和 让分胜负
    }else if (self.type == 1 || self.type == 2 || self.type == 3){
        
        ByBasketBallSFCell *cell = [ByBasketBallSFCell cellWithTableView:tableView];
        if (self.type == 1) {
            cell.dataArray = rowSfArray;
        }else if (self.type == 2){
            cell.dataArray = rowRsfArray;
        }else if (self.type == 3){
            cell.dataArray = rowBigSmallArray;
        }
        
        cell.indexAtView = ^(NSInteger index){
            if (self.type == 1) {
                
                ByNetModel *netModel = [rowSfArray objectAtIndex:index];
                netModel.selected = !netModel.selected;
                
            }else if (self.type == 2){
        
                ByNetModel *netModel = [rowRsfArray objectAtIndex:index];
                netModel.selected = !netModel.selected;
                
            }else{
                
                ByNetModel *netModel = [rowBigSmallArray objectAtIndex:index];
                netModel.selected = !netModel.selected;
            }
            
            [self.tableView reloadData];
            [self updateBottomState];
           
        };
        
        return cell;
        
    }else if (self.type == 4){
        
        
        ByBasketBallSFCCell *cell = [ByBasketBallSFCCell cellWithTableView:tableView];
        cell.dataArray = rowSfcArray;
        cell.indexAtView = ^(NSInteger index){
            ByNetModel *netModel = [rowSfcArray objectAtIndex:index];
            netModel.selected = !netModel.selected;
            [self.tableView reloadData];
            [self updateBottomState];
        };
        return cell;
    }
    
    
    
    static NSString *cellIdentifier = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    }
    [cell.contentView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    return cell;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (self.type == 0 || self.type == 4) {
        return  85 + 81 + 30;
    }else{
        return 85 + 81 ;
    }
}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 30;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    ByFootBallSecitionView *header = [[ByFootBallSecitionView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 30)];
    [header addTarget:self action:@selector(sectionAction:) forControlEvents:UIControlEventTouchUpInside];
    header.tag = 100 + section;
    header.timeLabel.text = [self.timesArray objectAtIndex:section];
    
    NSMutableArray *sectionAry = [self.sfArray objectAtIndex:section];
    header.countLabel.text = [NSString stringWithFormat:@"共%ld场比赛", sectionAry.count];
    
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:header.countLabel.text];
    //设置颜色
    [attributedString addAttribute:NSForegroundColorAttributeName value:BY_THEME_COLOR range:[header.countLabel.text rangeOfString:[NSString stringWithFormat:@"%ld", sectionAry.count]]];
    [attributedString addAttribute:NSFontAttributeName value:B_FONT(16) range:[header.countLabel.text rangeOfString:[NSString stringWithFormat:@"%ld", sectionAry.count]]];
    header.countLabel.attributedText = attributedString;
    
    
    
    return header;
}

- (void)sectionAction:(UIControl *)conrol{
    NSInteger section = conrol.tag-100;
    if([self.sectionStateArray[section] integerValue]==0){
        [self.sectionStateArray replaceObjectAtIndex:section withObject:@(1)];
    }else{
        
        [self.sectionStateArray replaceObjectAtIndex:section withObject:@(0)];
    }
    
    [self.tableView reloadData];
}



- (void)updateBottomState{
    
    NSMutableArray *selectedArray1 = [ByFootBallHandle getFootBallSelectedDataWithArray:[self.sfArray copy]];
    NSMutableArray *selectedArray2 = [ByFootBallHandle getFootBallSelectedDataWithArray:[self.rsfArray copy]];
    NSMutableArray *selectedArray3 = [ByFootBallHandle getFootBallSelectedDataWithArray:[self.bigSmallArray copy]];
    NSMutableArray *selectedArray4 = [ByFootBallHandle getFootBallSelectedDataWithArray:[self.sfcArray copy]];
    
    
    NSMutableDictionary *spfDic =  [ByBasketBallHandle getFootBallDictionaryOfMatch:selectedArray1];
    NSMutableDictionary *rspfDic =  [ByBasketBallHandle getFootBallDictionaryOfMatch:selectedArray2];
    NSMutableDictionary *halfAllDic = [ByBasketBallHandle getFootBallDictionaryOfMatch:selectedArray3];
    NSMutableDictionary *goalsDic =  [ByBasketBallHandle getFootBallDictionaryOfMatch:selectedArray4];
    
    
    NSMutableArray *keysArray = [NSMutableArray arrayWithCapacity:1];
    
    NSArray *spfKeys = [spfDic allKeys];
    NSArray *rspfKeys = [rspfDic allKeys];
    NSArray *halfAllKeys = [halfAllDic allKeys];
    NSArray *goalsKeys = [goalsDic allKeys];
    
    
    [keysArray addObjectsFromArray:spfKeys];
    [keysArray addObjectsFromArray:rspfKeys];
    [keysArray addObjectsFromArray:halfAllKeys];
    [keysArray addObjectsFromArray:goalsKeys];
   
    NSSet *set = [NSSet setWithArray:keysArray];
    self.numberOfMatch = set.count;
    self.bottomView.countLabel.text = [NSString stringWithFormat:@"已选%ld场比赛", set.count];
}


- (void)getSelectedCountReloadBtn:(UIButton *)btn AtIndexPath:(NSIndexPath *)indexPath{
    
    
    NSInteger count = 0;
    NSMutableArray *sfSectionAry = [self.sfArray objectAtIndex:indexPath.section];
    NSMutableArray *rsfSectionAry = [self.rsfArray objectAtIndex:indexPath.section];
    NSMutableArray *bigSectionAry = [self.bigSmallArray objectAtIndex:indexPath.section];
    NSMutableArray *sfcSectionAry = [self.sfcArray objectAtIndex:indexPath.section];
    
    NSMutableArray *sfRowAry = [sfSectionAry objectAtIndex:indexPath.row];
    NSMutableArray *rsfRowAry = [rsfSectionAry objectAtIndex:indexPath.row];
    NSMutableArray *bigRowAry = [bigSectionAry objectAtIndex:indexPath.row];
    NSMutableArray *sfcRowAry = [sfcSectionAry objectAtIndex:indexPath.row];

    
    NSMutableArray *selectedArray1 = [ByLotteryAlgorithmHandle getSelectedDataWithArray:[sfRowAry copy]];
    NSMutableArray *selectedArray2 = [ByLotteryAlgorithmHandle getSelectedDataWithArray:[rsfRowAry copy]];
    NSMutableArray *selectedArray3 = [ByLotteryAlgorithmHandle getSelectedDataWithArray:[bigRowAry copy]];
    NSMutableArray *selectedArray4 = [ByLotteryAlgorithmHandle getSelectedDataWithArray:[sfcRowAry copy]];
    count = selectedArray1.count + selectedArray2.count + selectedArray3.count + selectedArray4.count;
    if (count == 0){
        [btn setTitle:@"更多\n玩法" forState:UIControlStateNormal];
    }else{
        [btn setTitle:[NSString stringWithFormat:@"已选\n%ld项", count] forState:UIControlStateNormal];
    }
    
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
